if {"bench" ni [info commands bench]} {
	package require bench
	namespace import bench::*
}

package require rl_json

namespace import rl_json::json

proc main {} {
bench template-1.1 {Basic templated document creation} -setup { #<<<
	set tmpl {
		{
			"foo": "~S:foo",
			"bar": "~S:a(bar)",
			"baz": "~N:baz"
		}
	}
	set foo	Foo
	array set a {
		foo		X
		bar		Bar
	}
} -compare {
	dict {
		json template_string $tmpl {
			foo		Foo
			a(bar)	Bar
		}
	}

	dict_lit {
		json template_string {
			{
				"foo": "~S:foo",
				"bar": "~S:a(bar)",
				"baz": "~N:baz"
			}
		} {
			foo		Foo
			a(bar)	Bar
		}
	}

	variables {
		json template_string $tmpl
	}

	dict_direct {
		json template $tmpl {
			foo		Foo
			a(bar)	Bar
		}
	}

	dict_lit_direct {
		json template {
			{
				"foo": "~S:foo",
				"bar": "~S:a(bar)",
				"baz": "~N:baz"
			}
		} {
			foo		Foo
			a(bar)	Bar
		}
	}

	variables_direct {
		json template $tmpl
	}
} -cleanup {
	unset -nocomplain tmpl a foo
} -result [json normalize {
	{
		"foo": "Foo",
		"bar": "Bar",
		"baz": null
	}
}]
#>>>
bench template-2.1 {Test templated doc creation, including numbers} -setup { #<<<
	set foo		1
	set bar		42.5
	set baz		1e6
	set quux	0x42
	set octal	077
	set space	" 42"

	set d {
		foo		1
		bar		42.5
		baz		1e6
		quux	0x42
		octal	077
		space	" 42"
	}
} -compare {
	template_string {
		json template_string {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		}
	}

	template_string_dict {
		json template_string {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		} $d
	}

	template {
		json template {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		}
	}

	template_dict {
		json template_string {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		} $d
	}
} -cleanup {
	unset -nocomplain foo bar baz d
} -result [json normalize {
	{
		"foo":		1,
		"bar":		42.5,
		//"baz":		1e6,
		"quux":		66,
		"octal":	63,
		"space":	42
	}
}]
#>>>
bench template-3.1 {Test templated doc creation, including numbers, json set on result} -setup { #<<<
	set foo		1
	set bar		42.5
	set baz		1e6
	set quux	0x42
	set octal	077
	set space	" 42"

	set d {
		foo		1
		bar		42.5
		baz		1e6
		quux	0x42
		octal	077
		space	" 42"
	}
} -compare {
	template_string {
		set j	[json template_string {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		}]
		json set j new null
	}

	template_string_dict {
		set j	[json template_string {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		} $d]
		json set j new null
	}

	template {
		set j [json template {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		}]
		json set j new null
	}

	template_dict {
		set j [json template {
			{
				"foo":		"~N:foo",
				"bar":		"~N:bar",
				//"baz":		"~N:baz",
				"quux":		"~N:quux",
				"octal":	"~N:octal",
				"space":	"~N:space"
			}
		} $d]
		json set j new null
	}
} -cleanup {
	unset -nocomplain foo bar baz d
} -result [json normalize {
	{
		"foo":		1,
		"bar":		42.5,
		//"baz":		1e6,
		"quux":		66,
		"octal":	63,
		"space":	42,
		"new":		null
	}
}]
#>>>
}
main

# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
